package cn.hznc.controller.abutment;

import cn.hznc.controller.task.InventoryTask;
import cn.hznc.controller.service.order.AbutmentService;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.excel.*;
import cn.hznc.exception.ToolLifeCycleException;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author：Administrator
 * @Date：2023/8/3 15:57
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/abutment")
public class AbutmentController{

    @Autowired
    private AbutmentService abutmentService;

    @Resource
    private InventoryTask inventoryTask;

    /**
     * 按月份导出工艺质量管理系统入库记录
     * @param response
     */
    @RequestMapping(value = "/exportAbutmentInboundRecords", method = RequestMethod.GET)
    public void exportAbutmentInboundRecords(HttpServletResponse response, Integer monthIndex) {
        try {
            abutmentService.exportAbutmentInboundRecords(response, monthIndex);
        } catch (IOException e) {
            log.error("exportToolForkInfo error========》msg：{}" + e.getMessage());
        }
    }

    /**
     * 按月份导出工艺质量管理系统出库记录
     * @param response
     */
    @RequestMapping(value = "/exportAbutmentOutboundRecords", method = RequestMethod.GET)
    public void exportAbutmentOutboundRecords(HttpServletResponse response, Integer monthIndex) {
        try {
            abutmentService.exportAbutmentOutboundRecords(response, monthIndex);
        } catch (IOException e) {
            log.error("exportToolForkInfo error========》msg：{}" + e.getMessage());
        }
    }

    /**
     * 按月份对接sap系统库存记录
     */
    @RequestMapping(value = "/abutmentEntryExitRecord", method = RequestMethod.GET)
    public ResponseBody abutmentEntryExitRecord(@RequestParam("startTime") String startTime , @RequestParam("endTime") String endTime) {
            return ResponseBody.success(abutmentService.abutmentEntryExitRecord(startTime, endTime));
    }

    /**
     * 按月份对接sap系统库存
     */
    @RequestMapping(value = "/abutmentEntryExit", method = RequestMethod.GET)
    public ResponseBody abutmentEntryExit(@RequestParam("startTime") String startTime , @RequestParam("endTime") String endTime) {
        try {
            return ResponseBody.success(abutmentService.abutmentEntryExit(startTime, endTime));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode() , e.getMsg());
        }
    }

    /**
     * 导入一级库出库记录
     */
    @RequestMapping(value = "/importFirstCabinetOutbound", method = RequestMethod.POST)
    public ResponseBody importFirstCabinetOutbound(@RequestPart("file") MultipartFile file) {
        try {
            FirstCabinetOutboundReadListener firstCabinetOutboundReadListener = new FirstCabinetOutboundReadListener();
            //FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), FirstCabinetOutboundImport.class, firstCabinetOutboundReadListener).headRowNumber(1).autoCloseStream(true).sheet().doRead();
            return ResponseBody.success(abutmentService.importFirstCabinetOutbound(firstCabinetOutboundReadListener.getImportData()));
        }  catch (IOException e) {
            return ResponseBody.error("1001" , "导入文件错误");
        }
    }

//    /**
//     * 导入一级库库存记录
//     */
//    @RequestMapping(value = "/test", method = RequestMethod.GET)
//    public ResponseBody test() {
//        //每晚11点半做库存快照
//        inventoryTask.inventorySnapshotTask();
//        return ResponseBody.success();
//    }

    /**
     * 导入一级库库存记录
     */
    @RequestMapping(value = "/importFirstCabinetStock", method = RequestMethod.POST)
    public ResponseBody importFirstCabinetStock(@RequestPart("file") MultipartFile file , @RequestPart("operator") String operator) {
        try {
            FirstCabinetStockReadListener firstCabinetStockReadListener = new FirstCabinetStockReadListener();
            //FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), FirstCabinetStockImport.class, firstCabinetStockReadListener).headRowNumber(1).autoCloseStream(true).sheet().doRead();
            return ResponseBody.success(abutmentService.importFirstCabinetStock(firstCabinetStockReadListener.getImportData() , operator));
        }  catch (IOException e) {
            return ResponseBody.error("1001" , "导入文件错误");
        }
    }
}
